home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 660 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.1 KB

  1. Path: gate.net!pslfl2-10
  2. From: bhutto@gate.net (William Hutto)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: gets() question
  5. Date: 8 Jan 1996 08:33:44 GMT
  6. Organization: CyberGate, Inc.
  7. Message-ID: <4cqkt8$1quo@news.gate.net>
  8. References: <4cosgf$rir@newsbf02.news.aol.com>
  9. NNTP-Posting-Host: pslfl2-10.gate.net
  10. X-Newsreader: News Xpress Version 1.0 Beta #4
  11.  
  12. In article <4cosgf$rir@newsbf02.news.aol.com>,
  13.    simpsondg@aol.com (SimpsonDG) wrote:
  14. >Can any of you C gurus out there help me with this?  I have a problem with
  15. >gets() that I don't understand.  The program below will successfully input
  16. >the string s[0], but will simply ignore the gets() that inputs s[1] and
  17. >goes on to ask for i[1].  What's the deal?  A little experimenting seems
  18. >to indicate that the problem arises when I have a gets() following a
  19. >scanf() -- e.g., a program using only gets() or only scanf() works fine.
  20. >
  21. >David Simpson
  22. >
  23. >--------------------------------------------------------------------------
  24. >-------------------
  25. >
  26. >#include "stdio.h"
  27. >
  28. >void main(void)
  29. >{
  30. >   int i[5];
  31. >   char s[3][10];
  32. >
  33. >   printf ("Enter s[0]:  ");
  34. >   gets (s[0]);
  35.           ^^^^        
  36.  
  37. Your compiler didn't generate an error? *s* is not an array of pointers to 
  38. arrays of chars as in argv[]. Now I remember why I always like being explicit. 
  39. Try this:
  40.  
  41.     fgets(&s[0][0],9,stdin);
  42.  
  43. You can use a 2 dimensional char array, except that you have to be complete 
  44. in your form and s[0] is not. Because fgets() (or gets() for that matter) is 
  45. looking for an address, you need to oblige, hence the prefix of *&*. The 
  46. reason for fgets() is to prevent overflowing your array by including a limit. 
  47. I strongly suggest you read the FAQ. You have several situations (including 
  48. the use of the dreaded scanf() and void main()) that need thorough 
  49. understanding. I think it's important that people understand that the FAQ for 
  50. comp.lang.c is not just "answers to frequently asked questions," as it might 
  51. be in other newsgroups, but an important "fill in the cracks" text that 
  52. belongs with the best of your C references. As a matter of fact, get the book 
  53. when you can.
  54.  
  55. Bill
  56.  
  57. "Whatcha got on?...Your mind?"
  58.